//index.js
//获取应用实例
var util = require('../../utils/util.js');
var app = getApp();

Page({
  data: {
    articleInfo: {},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    userInfo: {},
    pageNum: 1,
    isFirstLoad: true,
    noMore: false,
    curr_page_no: 1,
    totalPages: 0,
  },
  onShow: function() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 500
    })
    setTimeout(function () {
      wx.hideToast()
    }, 500);
  },
  onLoad: function () {
    this.resetData(true);
  },
  resetData: function (isRefresh) {
    var that = this;
    var pageNo = 1;
    if (!isRefresh) {
      //加载更多
      pageNo = that.data.curr_page_no + 1;
    }
    util.post(app.api.getArticle, { page: pageNo, pageSize: 10 }, function (res) {
      if (isRefresh) {
        //执行刷新 当前页号重置为1
        that.setData({
          curr_page_no: 1,
        });

        if (res == null && res.data == null && res.data.data.length < 0) {
          //刷新，没有数据
          wx.showToast({
            title: '没有数据',
            icon: 'loading',
            duration: 1200
          })
          that.setData({
            articleInfo: null,
            totalPages: 0,
            noMore: true
          });
        }else{
          //刷新，有数据
          that.setData({
            articleInfo: res.data.data,
            totalPages: res.data.pageNum,
            noMore: false
          });
        }

      } else {
        //加载更多
   
        that.setData({
          curr_page_no: pageNo
        });

        if (res == null && res.data == null && res.data.data.length < 0) {
          //更多，没有数据
     
          wx.showToast({
            title: '没有更多数据',
            icon: 'loading',
            duration: 1200
          })
          that.setData({
            noMore: true
          });
        } else {
    
         
          var dtList = res.data.data;
       
          var result = that.data.articleInfo;
          for (var j = 0; j < dtList.length;j++){
            result.push(dtList[j]);
          }
          var tmpNo = that.data.pageNo;
          tmpNo = tmpNo + 1;
          //更多，有数据
          that.setData({
            pageNo:tmpNo,
            articleInfo: result,
            totalPages: res.data.pageNum,
            noMore: false
          });
        }
      }
    });
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    var that = this;
    // 显示导航栏loading  
    wx.showNavigationBarLoading();
    // 调用接口加载数据  
    this.resetData(true);
    // 隐藏导航栏loading  
    wx.hideNavigationBarLoading();
    // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新  
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.curr_page_no >= that.data.totalPages) {
      //已经是最后一页
      that.setData({
        noMore: true,
      });
    } else {
      this.resetData(false);
    }
  },
})
